<?php

namespace App\Http\Controllers\Admin;

use App\Model\Cate;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CateController extends Controller
{
    /**
     * 分页列表页面
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $cate = (new Cate())
                ->tree();
       // dd($cate);
        $i = 1;
        return view('admin.cate.list',compact('cate','i'));
    }

    /**
     * 分类信息添加页面
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //查询顶级分类
        $info = Cate::where('cate_pid',0)->get();
        return view('admin.cate.create',compact('info'));
    }

    /**
     * 接收表单数据，添加分类信息
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //接收表单数据
        $info = $request->all();
        // dd($info);

        $res = Cate::insert($info,true);

        if ($res) {
            # code...
            $data = [
                'status'=>0,
                'message'=>'添加成功'
            ];
        } else {
            $data = [
                'status'=>1,
                'message'=>'添加失败'
            ];
        }
        return $data;
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $cate = Cate::find($id);
        $info = Cate::where('cate_pid',0)->get();
        return view('admin.cate.edit',compact('cate','info'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $info = $request->all();
        //更新当前分类信息
        $res = Cate::where('id', $id)
            ->update($info,true);
        //判断执行结果，将状态返回至页面
        if ($res) {
            # code...
            $data = [
                'status' => 0,
                'message' => '修改成功'
            ];
        }else{
            $data = [
                'status' => 1,
                'message' => '修改失败'
            ];
        }
        return $data;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
         $res = Cate::where('id', $id)
            ->delete();
        //判断执行结果，将状态返回至页面
        if ($res) {
            # code...
            $data = [
                'status' => 0,
                'message' => '删除成功'
            ];
        }else{
            $data = [
                'status' => 1,
                'message' => '删除失败'
            ];
        }
        return $data;
    }

    public function changeOrder(Request $request)
    {
        $info = $request->except('_token');
        // dd($info);

        $res = Cate::where('id',$info['id'])
                ->update(['cate_order'=>$info['cate_order']]);

        if ($res) {
            # code...
            $data = [
                'status' => 0,
                'message' => '修改成功'
            ];
        }else{
            $data = [
                'status' => 1,
                'message' => '修改失败'
            ];
        }
        return $data;
    }
}
